<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("conn.php");
$db = new DBC();
$cvs_file = $_FILES ['csv_file'] ['tmp_name'];
if ($cvs_file != "") {
    $num = 0;
    if (($handle = fopen($cvs_file, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if (count($data) > 0) {
                $code = trim($data[0]);
                if ($code != "" && startWith($code, "P")) {
                    $name = convertEncoding($data[10]);
                    $answer1 = $data[11] . "-" . (checkTrim($data[12]) == "" ? $data[11] : $data[12]);
                    $answer2 = $data[13] . "-" . (checkTrim($data[14]) == "" ? $data[13] : $data[14]);
                    $answer3 = $data[15] . "-" . (checkTrim($data[16]) == "" ? $data[15] : $data[16]);
                    $answer4 = $data[17] . "-" . (checkTrim($data[18]) == "" ? $data[17] : $data[18]);
                    $voice1 = $data[19];
                    $voice2 = $data[20];
                    $pic = $data[21];
                    $difficult = $data[22];
                    $version = $data[23];
                    $sql = "SELECT code FROM t_game_question WHERE code = '" . $code . "'";
                    $temp = $db->fetchData($sql);
                    if ($temp != false && count($temp) > 0) {
                        $sql = "UPDATE t_game_question SET name='" . $name . "',status = 0,answer1='" . $answer1 . "',answer2='" . $answer2 . "',answer3='" . $answer3 . "',answer4='" . $answer4 . "',voice1='" . $voice1 . "',voice2='" . $voice2 . "',pic='" . $pic . "',difficult='" . $difficult . "',version='" . $version . "',updateTime=now() WHERE code='" . $code . "'";
                    } else {
                        $sql = "INSERT INTO t_game_question(code,name,answer1,answer2,answer3,answer4,voice1,voice2,pic,difficult,url,version,updateTime,createTime)"
                                . " VALUES('" . $code . "','" . $name . "','" . $answer1 . "','" . $answer2 . "','" . $answer3 . "','" . $answer4 . "','" . $voice1 . "','"
                                . $voice2 . "','" . $pic . "','" . $difficult . "','','" . $version . "',now(),now())";
                    }

                    if ($db->execute($sql)) {
                        $row = array();

                        $row['code'] = $code;
                        $row['name'] = $name;
                        $row['voice1'] = $voice1;
                        $row['voice2'] = $voice2;
                        $row['pic'] = $pic;
                        $row['answer1'] = $answer1;
                        $row['answer2'] = $answer2;
                        $row['answer3'] = $answer3;
                        $row['answer4'] = $answer4;
                        $row['difficult'] = $difficult;

                        createXML($row);
                    }
                }
            }
            $num++;
        }
        fclose($handle);
    }
}
echo "<script language='javascript'>location='questionList.php?menu=question';</script>";

function convertEncoding($name) {
    if (mb_check_encoding($name, "utf-8") != 1) {
        if (mb_check_encoding($name, "gbk") == 1) {
            $name = mb_convert_encoding($name, "utf-8", "gbk");
        } else {
            $name = mb_convert_encoding($name, "utf-8");
        }
    }
    return $name;
}

function createXML($row){
    $xml = '<?xml version="1.0" encoding="utf-8"?>';
    $xml = $xml.'<UserData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
    $xml = $xml.'<question>';
    $xml = $xml.'<code>'.$row['code'].'</code>';
    $xml = $xml.'<name>'.$row['name'].'</name>';
    $xml = $xml.'<answer1>'.$row['answer1'].'</answer1>';
    $xml = $xml.'<answer2>'.$row['answer2'].'</answer2>';
    $xml = $xml.'<answer3>'.$row['answer3'].'</answer3>';
    $xml = $xml.'<answer4>'.$row['answer4'].'</answer4>';
    $xml = $xml.'<voice1>'.$row['voice1'].'</voice1>';
    $xml = $xml.'<voice2>'.$row['voice2'].'</voice2>';
    $xml = $xml.'<pic>'.$row['pic'].'</pic>';
    $xml = $xml.'<difficult>'.$row['difficult'].'</difficult>';
    $xml = $xml.'</question>';
    $xml = $xml.'</UserData>';
    $filename = "../game/xml/" . $row['code'] .$row['difficult'] .".xml";
    $file = fopen($filename,"w");	
    fwrite($file,$xml);
    fclose($file);
}

function buildXML($row) {
    $doc = new DOMDocument('1.0', 'UTF-8');
    $doc->formatOutput = TRUE;
    $r = $doc->createElement("question");
    $doc->appendChild($r);
    $code = $doc->createElement("code");
    $code->appendChild($doc->createTextNode($row['code']));
    $r->appendChild($code);
    $name = $doc->createElement("name");
    $name->appendChild($doc->createTextNode($row['name']));
    $r->appendChild($name);
    $answer1 = $doc->createElement("answer1");
    $answer1->appendChild($doc->createTextNode($row['answer1']));
    $r->appendChild($answer1);
    $answer2 = $doc->createElement("answer2");
    $answer2->appendChild($doc->createTextNode($row['answer2']));
    $r->appendChild($answer2);
    $answer3 = $doc->createElement("answer3");
    $answer3->appendChild($doc->createTextNode($row['answer3']));
    $r->appendChild($answer3);
    $answer4 = $doc->createElement("answer4");
    $answer4->appendChild($doc->createTextNode($row['answer4']));
    $r->appendChild($answer4);
    $voice1 = $doc->createElement("voice1");
    $voice1->appendChild($doc->createTextNode($row['voice1']));
    $r->appendChild($voice1);
    $voice2 = $doc->createElement("voice2");
    $voice2->appendChild($doc->createTextNode($row['voice2']));
    $r->appendChild($voice2);
    $pic = $doc->createElement("pic");
    $pic->appendChild($doc->createTextNode($row['pic']));
    $r->appendChild($pic);
    $difficult = $doc->createElement("difficult");
    $difficult->appendChild($doc->createTextNode($row['difficult']));
    $r->appendChild($difficult);
    //echo $doc->saveXML();
    $file = "../game/xml/" . $row['code'] .$row['difficult'] .".xml";
    $doc->save($file);
}

function checkTrim($item) {
    if (isset($item)) {
        $item = trim($item);
    } else {
        $item = "";
    }
    return $item;
}

function startWith($str, $needle) {
    return strpos($str, $needle) === 0;
}
?>
